import axios from 'axios';
import { ElMessage, ElMessageBox } from "element-plus";

let workApi = axios.create({
    // baseURL: 'http://10.82.11.45:9207/'
    // baseURL: 'http://' + document.location.hostname +':'+ window.location.port + "/"
    baseURL: import.meta.env.DEV?'http://10.248.219.71:207/':'http://' + document.location.hostname +':'+ window.location.port + "/"
})
workApi.interceptors.request.use(async (config) => {
    config.headers["token"] = localStorage.getItem("token")
    if (config.method === 'delete') {
        let a = false
        await ElMessageBox.confirm(
            '该操作会永久删除数据,是否继续?',
            '警告',
            {
                confirmButtonText: '继续',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then(() => {
                a = true
        }).catch(() => {

        })
        return a?config:null
    }else {
        return config
    }

})

workApi.interceptors.response.use(res => {
    if (res.data["msg"] && res.data["tipElm"]) {
        ElMessage({
            message: res.data["tipElm"],
            type: 'success',
        })
    } else {
        if (res.data["tipElm"]) {
            ElMessage.error(res.data["tipElm"])
        }
    }
    return res.data
}, error => {
    console.log(error)
    return false;
})

export default workApi;